package com.base.cn.platform.os.controller.op.coupon;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.coupon.OpCouponShop;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author ll
 * @since 2018/05/09
 */
@Controller
@RequestMapping("/admin/op/coupon")
public class AdminOpCouponController extends AdminBaseController {

    @Autowired
    private OpCouponService opCouponService;
    @Autowired
    private OpCouponShopService opCouponShopService;

    /**
     * 去添加页面
     * @return
     */
    @RequestMapping("/toSaveOpCouponPage")
    public String toSaveOpCouponPage(){
        return resultPage("/op/coupon/coupon_create");
    }

    /**
     * 去列表页面
     * @return
     */
    @RequestMapping("/toOpCouponListPage")
    public String toOpCouponListPage(Model model,
                                     @RequestParam(value = "other",required = false,defaultValue = "0")Integer other){
        model.addAttribute("other",other);
        return resultPage("/op/coupon/coupon_list");
    }

    /**
     * 分页搜索优惠券
     * @param opCoupon
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryOpCouponListPage")
    public String queryOpCouponListPage(Model model,
                                        @RequestBody Map<String, Object> opCoupon,
                                        @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap =opCouponService.queryOpCouponListPage(opCoupon,currentPage,true,true,true,false);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("other",opCoupon.get("other"));
        return resultPage("/op/coupon/coupon_ajax_list");
    }

    /**
     * 添加优惠券
     * @param coupon
     * @return
     */
    @RequestMapping("/saveOpCoupon")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加优惠券")
    public Map<String,Object> saveOpCoupon(@RequestBody Map<String,Object> coupon){
        return opCouponService.saveOpCoupon(coupon);
    }

    /**
     * 修改状态
     * @param couponIds
     * @param status
     * @return
     */
    @RequestMapping("/updateOpCouponStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改优惠券状态")
    public Map<String,Object> updateOpCouponStatus(@RequestParam("couponIds")String couponIds,
                                                   @RequestParam("status")Integer status){
        return opCouponService.updateOpCouponStatus(couponIds,status);
    }


    /***
     * 优惠券详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/toOpCouponInfo")
    public String toOpCouponInfo(Model model,
                                 @RequestParam("id")BigDecimal id){
        Map<String,Object> opCoupon = opCouponService.queryOpCouponById(id,true,true,true,false);
        model.addAttribute("coupon",opCoupon);
        return resultPage("/op/coupon/coupon_info");
    }

    /**
     * 赠送Coupon优惠券
     * @param userIds
     * @return
     */
    @RequestMapping("/presentOpCoupon")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "赠送学员优惠券")
    public Map<String,Object> presentOpCoupon(@RequestParam("userIds")String userIds,
                                              @RequestParam("couponId")BigDecimal couponId){
        userIds = StringUtils.subHeadTailString(userIds,",");
        return opCouponService.presentOpCoupon(userIds,couponId);
    }

    /**
     * 获取优惠券关联的商品数据
     * @param couponId 优惠券ID
     * @return Map<String,Object>
     */
    @RequestMapping("/showCouponShopData")
    @ResponseBody
    public Map<String,Object> showCouponShopData(@RequestParam("couponId") BigDecimal couponId){
        List<OpCouponShop> couponShopList = opCouponShopService.showCouponShopData(couponId);
        return ResultUtil.SUCCESS(couponShopList);
    }

    /**
     * 分页搜索优惠券
     * @param opCoupon
     * @param currentPage
     * @return
     */
    @RequestMapping("/querySelectCouponListPage")
    public String querySelectCouponListPage(Model model,
                                        @RequestBody Map<String, Object> opCoupon,
                                        @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        opCoupon.put("conditions",11);
        opCoupon.put("status",1);
        Map<String,Object> dataMap =opCouponService.queryOpCouponListPage(opCoupon,currentPage,true,true,true,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/coupon/ajax_select_coupon_list");
    }
}
